﻿using System;
using System.Data;
using System.Text;

namespace YueRu.Common
{
    public static class JsonHelper
    {
        /// <summary>
        /// Json特符字符过滤，参见http://www.json.org/
        /// </summary>
        /// <param name="sourceStr">要过滤的源字符串</param>
        /// <returns>返回过滤的字符串</returns>
        public static string JsonCharFilter(string sourceStr)
        {
            sourceStr = sourceStr.Replace("\\", "\\\\");
            sourceStr = sourceStr.Replace("\b", "\\\b");
            sourceStr = sourceStr.Replace("\t", "\\\t");
            sourceStr = sourceStr.Replace("\n", "\\\n");
            sourceStr = sourceStr.Replace("\n", "\\\n");
            sourceStr = sourceStr.Replace("\f", "\\\f");
            sourceStr = sourceStr.Replace("\r", "\\\r");
            return sourceStr.Replace("\"", "\\\"");
        }

        /// <summary>
        /// 将数据表转换成JSON类型串
        /// </summary>
        /// <param name="dt">要转换的数据表</param>
        /// <param name="isDispose">数据表转换结束后是否dispose掉</param>
        /// <returns></returns>
        public static StringBuilder DataTableToJson(DataTable dt, bool isDispose = false)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("[\r\n");

            //数据表字段名和类型数组
            var field = new string[dt.Columns.Count];
            var i = 0;
            var formatStr = "{{";
            foreach (DataColumn dc in dt.Columns)
            {
                field[i] = dc.Caption.ToLower().Trim();
                formatStr += "'" + dc.Caption.ToLower().Trim() + "':";
                var fieldtype = dc.DataType.ToString().Trim().ToLower();


                if (fieldtype.IndexOf("int") > 0 || fieldtype.IndexOf("deci") > 0 ||
                    fieldtype.IndexOf("floa") > 0 || fieldtype.IndexOf("doub") > 0 ||
                    fieldtype.IndexOf("bool") > 0)
                {
                    formatStr += "{" + i + "}";
                }
                else
                {
                    formatStr += "'{" + i + "}'";
                }
                formatStr += ",";
                i++;
            }

            if (formatStr.EndsWith(","))
            {//去掉尾部","号
                formatStr = formatStr.Substring(0, formatStr.Length - 1);
            }

            formatStr += "}},";

            i = 0;
            var objectArray = new object[field.Length];
            foreach (DataRow dr in dt.Rows)
            {
                foreach (string fieldname in field)
                {
                    //对 \ , ' 符号进行转换 
                    objectArray[i] = dr[field[i]].ToString().Trim().Replace("\\", "\\\\").Replace("'", "\\'");
                    switch (objectArray[i].ToString())
                    {
                        case "True":
                            {
                                objectArray[i] = "true";
                                break;
                            }
                        case "False":
                            {
                                objectArray[i] = "false";
                                break;
                            }
                        default:
                            break;
                    }
                    i++;
                }
                i = 0;
                stringBuilder.Append(String.Format(formatStr, objectArray));
            }
            if (stringBuilder.ToString().EndsWith(","))
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1); //去掉尾部","号
            }

            if (isDispose)
            {
                dt.Dispose();
            }

            return stringBuilder.Append("\r\n];");
        }

       
    }
}
